//
//  ViewController.m
//  05-ScrollView
//
//  Created by Snow on 16/7/20.
//  Copyright © 2016年 Snow. All rights reserved.
//

#import "ViewController.h"
//宏定义 命名两种方式
//宏嵌套
#define kScreenBounds [UIScreen mainScreen].bounds
//1.全大写
#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

//2.k + 驼峰命名
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height

@interface ViewController ()<UIScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:kScreenBounds];
    scrollView.backgroundColor = [UIColor cyanColor];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(10, 10, kScreenWidth - 20, 100);
    button.backgroundColor = [UIColor redColor];
    [scrollView addSubview:button];
    [self.view addSubview:scrollView];
    
    //1.contentSize 设置可以滚动的范围 默认CGSizezero
    scrollView.contentSize = CGSizeMake(990, 990);
    //2.contentOffset 偏移量 滚动的位置
    //不设置contentsize.contentoffSet 也可以改变
    //改变contentoffSet 改变了什么
    //答: 改变contentoffSet ,实质上是对scrollview的bounds进行修改
    NSLog(@"offset 改变前的frame: %@", NSStringFromCGRect(scrollView.frame));
    NSLog(@"offset 改变前的bounds: %@", NSStringFromCGRect(scrollView.bounds));
    scrollView.contentOffset = CGPointMake(0, 500);
    NSLog(@"offset 改变前后frame: %@", NSStringFromCGRect(scrollView.frame));
    NSLog(@"offset 改变后的bounds: %@", NSStringFromCGRect(scrollView.bounds));
    //3.添加滚动区域四周的滚动范围
    scrollView.contentInset = UIEdgeInsetsMake(10, 10, 10, 10);
    //4.锁定垂直或水平滚动 45° 锁会失效
    scrollView.directionalLockEnabled = YES;
    //5.回弹效果 默认yes 边缘有回弹效果
//    scrollView.bounces = NO;
    //6.alwaysBounceVertical在没有垂直方向的滚动范围时 保证垂直方向的回弹效果存在
    scrollView.alwaysBounceVertical = YES;
    //在没有水平方向的滚动范围时 保证回水平方向弹效果存在
    scrollView.alwaysBounceHorizontal = YES;
    //7.按页滚动
    scrollView.pagingEnabled = YES;
    //8.滚动视图能否滚动 默认yes
    scrollView.scrollEnabled = YES;
    //9.是否显示水平或垂直方向的滚动条
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = YES;
    //10.滚动指示器的边距
    scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(100, 0, 0, 0);
    //11.滑动指示器的颜色(深色背景,浅色背景)
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleWhite;
    //12.动画效果哦
//    scrollView.decelerationRate 减速
    //13.
//    scrollView.tracking 用户是否开始拖动
//    scrollView.dragging 用户是否正在拖动
//    scrollView.decelerating 是否减速中
    //14.双击状态栏 手势 默认 yes
    scrollView.scrollsToTop = YES;
    //15.代理
    scrollView.delegate = self;
    //16.键盘消失模式
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
    
    //测试
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 300, kScreenWidth - 20, 200)];
    textField.backgroundColor = [UIColor greenColor];
    [self.view addSubview:textField];
    
    //17.缩放系数
    //如果 想进行视图缩放 必须实现viewForZoomingInScrollView
    scrollView.minimumZoomScale = 0.5;
    scrollView.maximumZoomScale = 2;
}
//与其想要绑定的子视图
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
    return [scrollView.subviews firstObject];
}
//滚动视图滚动 就会执行
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //tracking 用户触摸
    if (scrollView.tracking) {
        NSLog(@"正在拖动滚动");
    }
    if (scrollView.dragging) {
        NSLog(@"用户开始滚动");
    }
    if (scrollView.decelerating)
    {
        NSLog(@"用户触摸结束");
    }
}
//将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    NSLog(@"开始拖拽");
}
//已经结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    NSLog(@"结束拖拽");
}
//将要开始进行减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"开始减速");
}

//已经结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    NSLog(@"已经减速");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
